﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task06
{
    class Tree
    {
        private List<Tree> childs;
        public int? Value;
        public List<Tree> Childs
        {
            get
            {
                if (childs == null)
                {
                    childs = new List<Tree>();
                }
                return childs;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            Tree tree = ReadTree(input);

            Console.WriteLine(IsBalanced(tree));
        }

        static Tree ReadTree(string input)
        {
            string substring = input.Substring(1, input.Length - 2);
            int index = 0;
            StringBuilder sb = new StringBuilder();
            while (index < substring.Length && ((index == 0 && substring[index] == '-') || char.IsDigit(substring[index])))
            {
                sb.Append(substring[index]);
                index++;
            }

            Tree tree;

            tree = new Tree()
            {
                Value = int.Parse(sb.ToString())
            };


            if (substring.Length >= 2 && substring[0] == '-' && char.IsDigit(substring[1]))
            {
                substring = substring.TrimStart("-0123456789".ToCharArray());
            }

            substring = substring.TrimStart("0123456789".ToCharArray());

            if (substring.Length >= 2 && (substring[0] == '>' || substring[1] == '>'))
            {
                substring = substring.TrimStart("->".ToCharArray());
            }


            if (substring.Length > 0)
            {
                List<string> branches = SplitBranches(substring);

                int leaf;
                foreach (var branch in branches)
                {
                    if (branch[0] == 'x')
                    {
                        tree.Childs.Add(new Tree()
                        {
                            Value = null
                        });
                    }
                    else
                    {
                        if (int.TryParse(branch, out leaf))
                        {
                            tree.Childs.Add(new Tree()
                            {
                                Value = leaf
                            });
                        }
                        else
                        {
                            tree.Childs.Add(ReadTree(branch));
                        }
                    }
                }
            }
            return tree;
        }

        static List<string> SplitBranches(string input)
        {
            List<string> split = new List<string>();
            int count = 0;
            StringBuilder sb = new StringBuilder();
            foreach (var ch in input)
            {
                if (ch == ',' && count == 0)
                {
                    split.Add(sb.ToString());
                    sb.Clear();
                    continue;
                }
                sb.Append(ch);
                if (ch == '(')
                {
                    count++;
                }
                if (ch == ')')
                {
                    count--;
                }
            }
            split.Add(sb.ToString());
            return split;
        }

        private static bool IsBalanced(Tree node)
        {
            Queue<Tree> queue = new Queue<Tree>();
            queue.Enqueue(node);
            int[] leftAndRightNodesCount = new int[2];
            int counter = 0;

            while (queue.Count > 0)
            {
                var nextNode = queue.Dequeue();

                foreach (var child in nextNode.Childs)
                {
                    queue.Enqueue(child);
                    leftAndRightNodesCount[counter] = child.Childs.Count;
                    counter++;
                }

                if (leftAndRightNodesCount[0] > leftAndRightNodesCount[1])
                {
                    if (leftAndRightNodesCount[0] - 1 != leftAndRightNodesCount[1])
                    {
                        return false;
                    }
                }
                else if (leftAndRightNodesCount[0] < leftAndRightNodesCount[1])
                {
                    if (leftAndRightNodesCount[0] != leftAndRightNodesCount[1] - 1)
                    {
                        return false;
                    }
                }
                counter = 0;
            }
            return true;
        }
    }
}
